<?php

class login extends a_front_controller {

    protected $form_data;
    protected $form_errors;
    protected $srv;

    public function __construct() {
        parent::__construct(true);
        $this->srv = new user_service_l_user();
        if(get_logged_user()){
            $this->do_redirect("<h2> You are already logged in!</h2>",2);
        }
        $this->__build_form();
    }

    public function batch_assign() {
        $this->assign("form_data", $this->form_data);
        $this->assign("form_error", $this->form_errors);
    }

    public function index() {        

    }

    protected function __build_form() {
        $this->form
                ->fieldset("Enter Information")
                ->open('user/login/process')
                ->text('username', 'Username', 'required|xss_clean|trim')
                ->pass('password', 'Password', 'required|xss_clean')
                ->indent(200)
                ->submit()
                ->reset();
        $this->form_data = $this->form->get();
        $this->form_errors = $this->form->errors;
    }

    public function process() {        
        if ($this->form->valid){
            $vars = $this->form->get_post();
           if( $this->srv->login($this->input->post('username'), $this->input->post('password'))!==NULL){
               event_dispatcher::i()->fire(module_u_user::EVENT_LOGIN_SUCCESS);
               $this->do_redirect("<h2 style='font-weight:normal'>You have successfully logged into the system</h2>..redirecting in 3 seconds..");
           }else{
                $this->form_errors = "<div id='errors' style='color:#CC0000'> - Could not login with given username and password!</div>";
           }
        }
        else{            
            $this->form_errors = $this->form->errors;
        }
    }

}

?>